<?php

namespace app\common\dao\system;

use app\common\model\system\Admin as model;
use app\common\dao\BaseDao;

class AdminDao extends BaseDao
{
    /**
    * 设置模型
    * @return model
    */
    protected function getModel(): string
    {
        return model::class;
    }


    public function search(array $where = [], $is_del = 0,$level = true)
    {
        $query = ($this->getModel())::getDB();
        if($level) $query->where('level', '<>', 0);
        $query->when($is_del !== null, function ($query) use ($is_del) {
            $query->where('is_del', $is_del);
        })->when(isset($where['date']) && $where['date'] !== '', function ($query) use ($where) {
            getModelTime($query, $where['date']);
        });
        if (isset($where['keyword']) && $where['keyword'] !== '') {
            $query = $query->whereLike('real_name|account', '%' . $where['keyword'] . '%');
        }
        if (isset($where['status']) && $where['status'] !== '') {
            $query = $query->where('status', intval($where['status']));
        }
        return $query;
    }

    public function exists(int $id)
    {
        $query = ($this->getModel())::getDB()->where($this->getPk(), $id)->where('is_del', 0);
        return $query->count() > 0;
    }



   /* public function get( $id)
    {
        return ($this->getModel())::getInstance()->where('is_del', 0)->find($id);
    }*/


    public function fieldExists($field, $value, ?int $except = null): bool
    {
        $query = ($this->getModel())::getDB()->where($field, $value)->where('is_del', 0);
        if (!is_null($except)) $query->where($this->getPk(), '<>', $except);
        return $query->count() > 0;
    }


    public function accountByAdmin(string $account)
    {
        return ($this->getModel())::getInstance()->where('account', $account)
            ->where('is_del', 0)
            ->field(['account', 'pwd', 'real_name', 'login_count', 'admin_id', 'status'])
            ->find();
    }


}